//
//  MLDTabBar.swift
//  Sina
//
//  Created by 万世豪情 on 15/10/11.
//  Copyright © 2015年 万世豪情. All rights reserved.
//

import UIKit

class MLDTabBar: UITabBar {
    
    var plusButtonClickBlock: (()->())?

    var plusButton: UIButton?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        let plusButton = UIButton()
        
        //设置背景图片
        plusButton.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        plusButton.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        //设置图片
        plusButton.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        plusButton.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        //设置大小
        plusButton.sizeToFit()
        
        addSubview(plusButton)
        
        self.plusButton = plusButton
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        //设置中心点
        self.plusButton?.center = CGPointMake(self.center.x, self.frame.size.height * 0.5)
        
        //每个按钮的宽
        let childW = frame.size.width / 5
        
        //定义角标
        var index = 0
        
        for childView in subviews {
            if
        childView.isKindOfClass(NSClassFromString("UITabBarButton")!) {
            //设置按钮的宽
            childView.width = childW
            //设置按钮的x
            childView.x = CGFloat(index) * childW
            
            index++
            
            if index == 2 {
            
                index++
                }
        
            }
        
        }
        
    }
    @objc private func pulsButtonClick() {
    
        if let block = self.plusButtonClickBlock {
        
            block()
        }
        
    }
    

}
